package com.vehicle_information_management_system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle_information_management_system.pojo.AdminInfo;
import com.vehicle_information_management_system.service.AdminInfoService;
import com.vehicle_information_management_system.mapper.AdminInfoMapper;
import com.vehicle_information_management_system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author huawei
* @description 针对表【admin_info】的数据库操作Service实现
* @createDate 2025-02-15 18:04:30
*/
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminInfoMapper, AdminInfo>
    implements AdminInfoService{

    @Autowired
    private AdminInfoMapper adminInfoMapper;
    @Override
    public AdminInfo login(String loginName, String password) {
        return adminInfoMapper.selectOne(new QueryWrapper<AdminInfo>().eq("login_name",loginName).eq("password", MD5.create().digestHex(password)));
    }

    @Override
    public void register(AdminInfo adminInfo) {
        AdminInfo admin=adminInfoMapper.selectOne(new QueryWrapper<AdminInfo>().eq("login_name",adminInfo.getLoginName()));
        if(admin!=null){
            throw new RuntimeException("用户名已存在");
        }
        else {
            adminInfo.setRole("admin");
            adminInfo.setPassword(MD5.create().digestHex(adminInfo.getPassword()));
            adminInfoMapper.insert(adminInfo);
        }

    }

    @Override
    public void update(AdminInfo adminInfo) {
        adminInfo.setPassword(MD5.create().digestHex(adminInfo.getPassword()));
        adminInfoMapper.updateById(adminInfo);
    }

    @Override
    public void delete(Integer id) {
        adminInfoMapper.deleteById(id);
    }

    @Override
    public IPage<AdminInfo> listPage(Integer pageNum, Integer pageSize, String search) {
        Page<AdminInfo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<AdminInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role","admin");
        if(StrUtil.isNotBlank(search)){
            queryWrapper.like("login_name",search);
        }
        return adminInfoMapper.selectPage(page,queryWrapper);
    }
}




